Option Explicit On

Imports System.Runtime.InteropServices
Imports System.Text

Public Class SpecialFolder
    Private Declare Function SHGetSpecialFolderPath Lib "SHELL32.DLL" Alias "SHGetSpecialFolderPathA" ( _
        ByVal hWndOwner As IntPtr, _
         ByVal lpszPath As StringBuilder, _
        ByVal nFolder As Integer, _
        ByVal fCreate As Boolean) As Integer

    Public Enum CSIDL As Integer
        ADMINTOOLS = &H30
        ALTSTARTUP = &H1D
        APPDATA = &H1A
        BITBUCKET = &HA
        CDBURN_AREA = &H3B
        COMMON_ADMINTOOLS = &H2F
        COMMON_ALTSTARTUP = &H1E
        COMMON_APPDATA = &H23
        COMMON_DESKTOPDIRECTORY = &H19
        COMMON_DOCUMENTS = &H2E
        COMMON_FAVORITES = &H1F
        COMMON_MUSIC = &H35
        COMMON_OEM_LINKS = &H3A
        COMMON_PICTURES = &H36
        COMMON_PROGRAMS = &H17
        COMMON_STARTMENU = &H16
        COMMON_STARTUP = &H18
        COMMON_TEMPLATES = &H2D
        COMMON_VIDEO = &H37
        COMPUTERSNEARME = &H3D
        CONNECTIONS = &H31
        CONTROLS = &H3
        COOKIES = &H21
        DESKTOP = &H0
        DESKTOPDIRECTORY = &H10
        DRIVES = &H11
        FAVORITES = &H6
        FLAG_CREATE = &H8000
        FLAG_DONT_VERIFY = &H4000
        FLAG_MASK = &HFF00
        FLAG_NO_ALIAS = &H1000
        FLAG_PER_USER_INIT = &H800
        FONTS = &H14
        HISTORY = &H22
        INTERNET = &H1
        INTERNET_CACHE = &H20
        LOCAL_APPDATA = &H1C
        MYDOCUMENTS = &HC
        MYMUSIC = &HD
        MYPICTURES = &H27
        MYVIDEO = &HE
        NETHOOD = &H13
        NETWORK = &H12
        PERSONAL = &H5
        PRINTERS = &H4
        PRINTHOOD = &H1B
        PROFILE = &H28
        PROGRAM_FILES = &H26
        PROGRAM_FILES_COMMON = &H2B
        PROGRAM_FILES_COMMONX86 = &H2C
        PROGRAM_FILESX86 = &H2A
        PROGRAMS = &H2
        RECENT = &H8
        RESOURCES = &H38
        RESOURCES_LOCALIZED = &H39
        SENDTO = &H9
        STARTMENU = &HB
        STARTUP = &H7
        SYSTEM = &H25
        SYSTEMX86 = &H29
        TEMPLATES = &H15
        WINDOWS = &H24
    End Enum

    Public Function GetSpecialFolderLocation(ByVal cID As CSIDL) As String
        Dim sPathName As StringBuilder = New StringBuilder(" ", 500)

        Dim iRet As Integer = SHGetSpecialFolderPath(IntPtr.Zero, sPathName, cID, False)

        If iRet = 0 Then
            Debug.WriteLine(Marshal.GetLastWin32Error)
        End If

        Return sPathName.ToString
    End Function
End Class
